<?php 
	session_start();
	if (!isset($_SESSION['name'])) {
		header("Location: login.php");
	}
	$db_connect = mysqli_connect("localhost","root","","tractor");
?>
<!DOCTYPE html>
<html>
<head>
	<title>Adding and item</title>
</head>

<body>
	<form method="post" action="add.php">
		<label for="name">Tractor name:</label>
		<input type="text" name="name" />
		<label for="model">Tractor model:</label>
		<input type="text" name="model" />
		<label for="price">Tractor price:</label>
		<input type="text" name="price" />

		<input type="submit" name="submit" value="ADD">
		<?php 
			if(isset($_POST['return'])){
				header("Location: index.php");
			}
			if(isset($_POST['submit']))
			{
				if(empty($_POST['name']))
				{
					echo "<p>Please insert a name</p>";
				}
				if(empty($_POST['model']))
				{
					echo "<p>Please insert a model</p>";
				}
				if(empty($_POST['price']))
				{
					echo "<p>Please insert a price</p>";
				}
				
				
				$name = $_POST['name'];
				$model = $_POST['model'];
				$price = $_POST['price'];

				if(isset($_SESSION['link'])){
					$link = $_SESSION['link'];
				}
				
				
				
				$query = "insert into tractors(name,model,price,link)
							values(\"$name\",\"$model\",\"$price\",\"$link\")";
				$result = mysqli_query($db_connect,$query);
				
				if($result)
				{
					echo "successfull";
				}
				else
				{
					echo "not added";
				}
			}
		?>
		<input type="submit" name="return" value="Back"/>
	</form>
	<form action="upload_file.php?from=add" method="post" enctype="multipart/form-data">
		<label for="file">Filename:</label>
		<input type="file" name="file" id="file"><br>
		<input type="submit" name="submit" value="Add Picture">
	</form>
</body>

</html>

<?php
	mysqli_close($db_connect);
?>



